package com.ruoyi.project.hcy.product.service.impl;

import com.ruoyi.common.exception.hcy.product.IllegalCountException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.product.domain.specification.ProSpecification;
import com.ruoyi.project.hcy.product.domain.style.ProStyle;
import com.ruoyi.project.hcy.product.mapper.ProSpecificationMapper;
import com.ruoyi.project.hcy.product.mapper.ProductMapper;
import com.ruoyi.project.hcy.product.service.IProSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ProSpecificationServiceImpl
 * @Author SAI
 * @Date 2020/9/18 13:58
 * @Description 规格业务实现类
 * @Version 1.0
 */
@Service("specification")
public class ProSpecificationServiceImpl implements IProSpecificationService {

    @Autowired
    private ProSpecificationMapper proSpecificationMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询规格集合
     * @param proSpecification  对象
     * @return  集合
     */
    @Override
    public List<ProSpecification> selectProSpecificationList(ProSpecification proSpecification) {
        return proSpecificationMapper.selectProSpecificationList(proSpecification);
    }

    /**
     * 检测规格名称是否唯一
     * @param proSpecification  对象
     * @return  0或1字符串
     */
    @Override
    public String checkSpecificationNameUnique(ProSpecification proSpecification) {
        Long specificationId = StringUtils.isNull(proSpecification.getSpecificationId()) ? -1L : proSpecification.getSpecificationId();
        ProSpecification info = proSpecificationMapper.checkSpecificationNameUnique(proSpecification.getSpecificationName());
        if (StringUtils.isNotNull(info) && info.getSpecificationId().longValue() != specificationId.longValue()) {
            return "1";
        }
        return "0";
    }

    /**
     * 新增规格
     * @param proSpecification 对象
     * @return int
     */
    @Override
    public int insertProSpecification(ProSpecification proSpecification) {
        return proSpecificationMapper.insertProSpecification(proSpecification);
    }

    /**
     * 根据id查询规格
     * @param specificationId  对象
     * @return  ProSpecification对象
     */
    @Override
    public ProSpecification selectProSpecificationById(Long specificationId) {
        return proSpecificationMapper.selectProSpecificationById(specificationId);
    }

    /**
     * 修改规格
     * @param proSpecification 对象
     * @return int
     */
    @Override
    public int updateProSpecification(ProSpecification proSpecification) {
        return proSpecificationMapper.updateProSpecification(proSpecification);
    }

    /**
     * 删除规格
     * @param ids  id集合
     * @return  int
     */
    @Override
    public int deleteProSpecificationByIds(String ids) {
        Long[] longs = Convert.toLongArray(ids);
        for (Long specificationId : longs) {
                if (productMapper.countSpecificationProductById(specificationId) > 0) {
                    ProSpecification proSpecification = proSpecificationMapper.selectProSpecificationById(specificationId);
                    throw new IllegalCountException(String.format("%1$s已被产品使用,不能删除", proSpecification.getSpecificationName()));
                }
            }
        return proSpecificationMapper.deleteProSpecificationByIds(longs);
    }
}
